package paixv;

public class CycleLink {
	Node head = null;
	Node temp = null;
	int length, k, m;

	public CycleLink(int length) {
		this.length = length;
	}

	public void createLink() {
		for (int i = 1; i <= length; i++) {
			Node node = new Node(i);
			if (i == 1) {
				this.head = node;
				this.temp = node;
			} else if (i == length) {
				temp.nextNode = node;
				temp = node;
				temp.nextNode = this.head;
			} else {
				temp.nextNode = node;
				temp = node;
			}

		}
	}

	public void play() {

		Node temp = this.head;
		for (int i = 1; i < k; i++) {// 从第k个人开始数数
			temp = temp.nextNode;
		}
		while (this.length != 1) {
			for (int j = 1; j < m - 1; j++) {// 数到第m-1个人
				temp = temp.nextNode;
			}
			System.out.println(temp.nextNode.no + "出圈");
			temp.nextNode = temp.nextNode.nextNode;
			temp = temp.nextNode;
			this.length--;
		}
		System.out.println("最后留下" + temp.no);
	}

	public void show() {
		Node node = this.head;
		do {
			System.out.print(node.no + " ");
			node = node.nextNode;
		} while (node != this.head);
	}

	public static void main(String[] args) {
		CycleLink cl = new CycleLink(6);
		cl.createLink();
		cl.show();
		cl.k = 2;
		cl.m = 2;
		cl.play();
	}

}

class Node {
	int no;
	Node nextNode;

	public Node(int no) {
		this.no = no;
	}
}